<!--
POST /back/practiceexams/PracticeExam
批量删除

POST /back/practiceexams/addExamContent
添加模考试卷题目

POST /back/practiceexams/addPracticeExam
添加模考试卷,填写试卷基本信息

POST /back/practiceexams/getPracticeExam
获取模考试卷详情

POST /back/practiceexams/queryExamList
获取模考试卷列表

POST /back/practiceexams/updatePracticeExam
编辑模考试 -->
<template>
  <el-main style="padding: 20px">
    <el-button type="primary" size="mini" icon="el-icon-plus" @click="tianjia = true">
      添加考试
    </el-button>
    <el-button type="primary" size="mini" @click="delList"> 删除选中 </el-button>
    <!-- 组合习题 -->
    <el-dialog :visible.sync="AddtEaxm" width="80%">
      <el-radio-group v-model="radio" style="margin-top: 20px" v-if="typeList">
        <el-radio
          :label="item.categoryCode"
          v-for="(item, index) in typeList"
          :key="index"
          >{{ item.categoryName }}</el-radio
        >
      </el-radio-group>
      <el-table
        :data="ExamList"
        ref="multipleTable"
        style="width: 100%"
        :row-key="getRowKeys"
        tooltip-effect="dark"
        @select="onTableSelect"
        @selection-change="selectEaxm"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="content" label="题目"></el-table-column>
        <el-table-column
          prop="type"
          label="类型（1-选择1 2-填空 3-简答 4-判断 5-多选 6-计算 7-论叙 8-操作）"
        ></el-table-column>
      </el-table>
      <el-pagination
        background
        layout="prev, pager, next"
        :page-count="ExamData.pages"
        :current-page="Examcur"
        @current-change="ExamQie"
      ></el-pagination>
      <div style="text-align: right">
        <el-button type="primary" size="mini" @click="tanchuExam()"> 确认选中 </el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="请填写分数"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-input v-model="point" placeholder="默认为1分"></el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="okExam">确 定</el-button>
      </span>
    </el-dialog>

    <div class="table-box">
      <el-table
        :data="list"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" align="left"> </el-table-column>
        <el-table-column prop="kemu" label="科目"></el-table-column>
        <el-table-column prop="examName" label="试卷名"></el-table-column>
        <el-table-column prop="explainTxt" label="说明"></el-table-column>
        <el-table-column prop="duration" label="考试时长"></el-table-column>
        <el-table-column prop="emaxDate" label="考试日期"></el-table-column>
        <el-table-column prop="examPeriod" label="考试时段"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button @click="addWord(scope.row)" type="text" size="mini"
              >组合习题</el-button
            >
            <el-button @click="addAutoWord(scope.row)" type="text" size="mini"
              >自动组题</el-button
            >
            <el-button @click="showEditView(scope.row)" type="text" size="mini"
              >查看</el-button
            >
            <el-button @click="setbianji(scope.row)" type="text" size="mini"
              >编辑</el-button
            >
            <el-button type="text" size="mini" @click="deleteObj(scope.row)"
              >删除</el-button
            >
            <el-button type="text" size="mini" @click="fenxi(scope.row)"
              >试卷分析</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="prev, pager, next"
        :page-count="dailyData.pages"
        :current-page="dailyCur"
        @current-change="dailyQie"
      ></el-pagination>
    </div>
    <el-dialog :visible.sync="showAddView" width="80%">
      <el-form class="demo-ruleForm" label-width="80px">
        <el-form-item label="科目">
          <!-- <el-select placeholder="请选择科目" v-model="val2">
            <el-option
              v-for="(item, index) in course"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select> -->
          <el-cascader
            :options="options"
            :props="optionProps"
            v-model="abc"
            :show-all-levels="false"
            @change="handleChange"
          >
          </el-cascader>
        </el-form-item>
        <el-form-item label="总分"
          ><el-input type="text" v-model="zongpoint"></el-input
        ></el-form-item>
        <el-form-item label="考试名">
          <el-input type="text" v-model="examName"></el-input>
        </el-form-item>
        <el-form-item label="考试说明"
          ><el-input type="textarea" v-model="explainTxt"></el-input
        ></el-form-item>
        <el-form-item label="考试时长"
          ><el-input type="textarea" v-model="duration"></el-input
        ></el-form-item>
        <el-form-item label="考试日期">
          <el-date-picker v-model="value1" type="date" placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="考试时间">
          <el-time-select
            placeholder="起始时间"
            v-model="startTime"
            :picker-options="{
              start: '08:30',
              step: '00:05',
              end: '22:30',
            }"
          >
          </el-time-select>
          <el-time-select
            placeholder="结束时间"
            v-model="endTime"
            :picker-options="{
              start: '08:30',
              step: '00:05',
              end: '23:30',
              minTime: startTime,
            }"
          >
          </el-time-select>
        </el-form-item>
      </el-form>
      <div class="block"></div>
      <el-button type="primary" size="mini" @click="setData()"> 编辑 </el-button>
    </el-dialog>

    <!-- 添加考试 -->
    <el-dialog :visible.sync="tianjia" width="80%">
      <el-form class="demo-ruleForm" label-width="80px">
        <el-form-item label="科目">
          <el-cascader
            :options="options"
            :props="optionProps"
            v-model="abc"
            :show-all-levels="false"
            @change="handleChange"
          >
          </el-cascader>
          <!-- <el-select placeholder="请选择科目" v-model="val2">
            <el-option
              v-for="(item, index) in course"
              :key="index"
              :label="item"
              :value="item.id"
            ></el-option>
          </el-select> -->
        </el-form-item>
        <el-form-item label="总分"
          ><el-input type="text" v-model="zongpoint"></el-input
        ></el-form-item>
        <el-form-item label="考试名">
          <el-input type="text" v-model="examName"></el-input>
        </el-form-item>
        <el-form-item label="考试说明"
          ><el-input type="textarea" v-model="explainTxt"></el-input
        ></el-form-item>
        <el-form-item label="考试时长"
          ><el-input type="textarea" v-model="duration"></el-input
        ></el-form-item>
        <el-form-item label="考试日期">
          <el-date-picker v-model="value1" type="date" placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="考试时间">
          <el-time-select
            placeholder="起始时间"
            v-model="startTime"
            :picker-options="{
              start: '08:30',
              step: '00:05',
              end: '22:30',
            }"
          >
          </el-time-select>
          <el-time-select
            placeholder="结束时间"
            v-model="endTime"
            :picker-options="{
              start: '08:30',
              step: '00:05',
              end: '23:30',
              minTime: startTime,
            }"
          >
          </el-time-select>
        </el-form-item>
      </el-form>
      <div class="block"></div>
      <el-button type="primary" size="mini" @click="addData()"> 添加 </el-button>
    </el-dialog>

    <!-- 自动组题 -->
    <el-dialog :visible.sync="autoTi" width="80%">
      <el-form class="demo-ruleForm" label-width="80px">
        <!-- <el-form-item label="章节ID"
          ><el-input type="text" v-model="chapterId"></el-input
        ></el-form-item> -->
        <el-form-item label="关键字">
          <el-input type="text" v-model="keyWords"></el-input>
        </el-form-item>
        <el-form-item label="难度等级"
          ><el-input
            type="textarea"
            placeholder="1-3,越大难度越高"
            v-model="level"
          ></el-input
        ></el-form-item>
        <el-form-item label="习题类型"
          ><el-input type="textarea" v-model="autoType"></el-input
        ></el-form-item>
      </el-form>
      <div class="block"></div>
      <el-button type="primary" size="mini" @click="tianjiaAuto()"> 添加 </el-button>
    </el-dialog>
    <el-dialog
  title="提示"
  :visible.sync="messagevisible"
  width="30%"
  :before-close="handleClose">
  <span>请输入对应的数量</span>
  <div style="display:flex;flex-direction: row;align-items: center;padding:10px">
  <span>选择题：</span> <el-input v-model="xdanxuan" style="width:50%" placeholder="请输入数量"  ></el-input>
  </div>
  <div style="display:flex;flex-direction: row;align-items: center;padding:10px">
  <span>填空题：</span> <el-input v-model="xtiankong" style="width:50%" placeholder="请输入数量"  ></el-input>
  </div>
  <div style="display:flex;flex-direction: row;align-items: center;padding:10px">
  <span>简答题：</span> <el-input v-model="xjianda" style="width:50%" placeholder="请输入数量"  ></el-input>
  </div>
  <div style="display:flex;flex-direction: row;align-items: center;padding:10px">
  <span>判断题：</span> <el-input v-model="xpanduan" style="width:50%" placeholder="请输入数量"  ></el-input>
  </div>
  <div style="display:flex;flex-direction: row;align-items: center;padding:10px">
  <span>多选题：</span> <el-input v-model="xduoxuan" style="width:50%" placeholder="请输入数量"  ></el-input>
  </div>
  <div style="display:flex;flex-direction: row;align-items: center;padding:10px">
  <span>计算题：</span> <el-input v-model="xjisuan" style="width:50%" placeholder="请输入数量"  ></el-input>
  </div>
  <div style="display:flex;flex-direction: row;align-items: center;padding:10px">
  <span>论叙题：</span> <el-input v-model="xlunxu" style="width:50%" placeholder="请输入数量"  ></el-input>
  </div>
  <div style="display:flex;flex-direction: row;align-items: center;padding:10px">
  <span>操作题：</span> <el-input v-model="xcaozuo" style="width:50%" placeholder="请输入数量"  ></el-input>
  </div>
  <span slot="footer" class="dialog-footer">
    <el-button @click="messagevisible = false">取 消</el-button>
    <el-button type="primary" @click="addmess">确 定</el-button>
  </span>
</el-dialog>

  </el-main>
</template>

<script>
export default {
  data() {
    return {
      autoTi: false,
      chapterId: "",
      keyWords: "",
      level: "",
      autoType: "",
      dailyCur: 1,
      dailyData: [],
      multipleSelection: [],
      radio: 1,
      duration: "",
      subjectId: 1,
      typeList: [],
      dialogVisible: false,
      showAddView: false,
      value: "",
      value1: "",
      startTime: "",
      endTime: "",
      explainTxt: "",
      examName: "",
      val2: "",
      tianjia: false,
      course: [],
      list: [],
      testid: 1,
      page: 1,
      size: 1,
      examPage: 1,
      examSize: 10,
      AddtEaxm: false,
      messagevisible:false,
      ExamList: [],
      ExamData: [],
      Examcur: 1,
      selectAdd: [],
      zancun: [],
      point: "",
      zongpoint: "",
      autoExamId: "",
      options: [],
      optionProps: {
        value: "classId",
        label: "className",
        children: "children",
      },
      abc: [],
      selected: "",
      danxuan: 0,
      tiankong : 0,
      jianda :0,
      panduan :0,
      duoxuan : 0,
      jisuan :0,
      lunxu :0,
      caozuo: 0,
      xdanxuan: 10,
      xtiankong : 10,
      xjianda :10,
      xpanduan :10,
      xduoxuan : 10,
      xjisuan :10,
      xlunxu :10,
      xcaozuo: 10,
      nowlist:{}
    };
  },
  inject: ["reload"],
  mounted() {
    this.queryCourse().then(this.getdata);
    this.getType();
  },
  watch: {
    radio() {
      this.page = 1;
      this.getEaxm();
    },
  },
  methods: {
    getRowKeys(row) {
      return row.id;
    },
    onTableSelect(rows, row) {
      this.selected = rows.length && rows.indexOf(row) !== -1;
      //console.log(this.selected);
    },
    //回显
    toggleSelection() {
      let xuanzhong = this.selectAdd;
      let rows = this.ExamList;
      this.$nextTick(() => {
        if (rows.length != 0) {
          rows.forEach((row) => {
            xuanzhong.forEach((xuan) => {
              if (row.practiceId == xuan.practiceId) {
                this.$refs.multipleTable.toggleRowSelection(row, true);
              }
            });
          });
        } else {
          this.$refs.multipleTable.clearSelection();
        }
      });
    },
    // 试卷分析
    fenxi(e) {
      //console.log(e);
      this.$router.push({ path: "/shop/examAnalysis?id=" + e.examId });
    },
    //
    handleChange() {
      this.val2 = this.abc[1];
      //console.log(this.val2);
    },
    //自动组题
    addAutoWord(e) {
      // //console.log(e);
      this.autoExamId = e.examId;
      this.autoTi = !this.autoTi;
      //console.log(this.autoExamId);
      //  this.postRequest('/back/practicestores/queryRandomList', { examIds: arr }).then(res => {
      //   //console.log(res);
      //   this.reload();
      //   this.$message(res.data.msg);
      // });
    },
    // 添加自动组题
    tianjiaAuto() {
      const params = {
        examId: this.autoExamId,
        keyWords: this.keyWords,
        level: this.level,
        autoType: this.autoType,
      };
      //console.log(params);
      this.postRequest("/back/practicestores/queryRandomList", {
        examId: this.autoExamId,
        keyWords: this.keyWords,
        level: this.level,
        autoType: this.autoType,
      }).then((res) => {
        //console.log(res);
        this.reload();
        this.$message(res.data.msg);
      });
    },
    dailyQie(val) {
      //console.log(val);
      this.page = val;
      this.getdata();
    },
    delList() {
      let arr = [];
      //console.log(this.multipleSelection);
      this.multipleSelection.forEach((item) => {
        arr.push(item.examId);
      });
      arr = arr.join(",");
      console.log(arr);
      this.postRequest("/back/practiceexams/PracticeExam", {
        examIds: arr,
      }).then((res) => {
        //console.log(res);
        this.reload();
        this.$message(res.data.msg);
      });
    },
    getType() {
      this.postRequest("/comm/queryCategoryByType", {
        type: "practice_type",
      }).then((res) => {
        //console.log(res);
        this.typeList = res.data.data;
        this.radio = this.typeList[0].categoryCode;
      });
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    okExam() {
      //555555555
      if (this.point == "") {
        this.point = 1;
      }
      let list = [];
      let that = this;
      this.selectAdd.forEach((item) => {
        let obj = {
          practiceId: item.practiceId,
          point: that.point,
        };
        list.push(obj);
      });
      //console.log(list, "12312");
      let listNew = [];
      this.postRequest(`/back/practiceexams/getPracticeExam?examId=${this.examId}`).then(
        (res) => {
          listNew = res.data.data.content;
          let arr = [];
          for (var item in listNew) {
            arr = arr.concat(listNew[item]);
          }
          //console.log(arr, "123123123123");
          arr.forEach((item) => {
            list.push({ practiceId: item.practiceId, point: item.point });
          });
          this.postJson(
            `/back/practiceexams/addExamContent?examId=${this.examId}`,
            list
          ).then((res) => {
            //console.log(res, "123123123");
            this.reload();
            this.$message(res.data.msg);
          });
        }
      );
    },
    tanchuExam() {
      this.danxuan = 0;
      this.tiankong = 0;
      this.jianda = 0;
      this.panduan = 0;
      this.duoxuan = 0;
      this.jisuan = 0;
      this.lunxu = 0;
      this.caozuo = 0;

      this.selectAdd.forEach((item) => {
        if (item.type == "1") {
          this.danxuan++;
        } else if (item.type == "2") {
          this.tiankong++;
        } else if (item.type == "3") {
          this.jianda++;
        } else if (item.type == "4") {
          this.panduan++;
        } else if (item.type == "5") {
          this.duoxuan++;
        } else if (item.type == "6") {
          this.jisuan++;
        } else if (item.type == "7") {
          this.lunxu++;
        } else if (item.type == "8") {
          this.caozuo++;
        }
      });
      //console.log(this.danxuan,this.duoxuan,this.panduan,this.jisuan)
      //console.log(this.selectAdd, "123123213");
      if(this.danxuan>this.xdanxuan){
         this.$message.error('单选数量超过，当前数量:'+this.danxuan+"最大可选数量:"+this.xdanxuan);
        return
      }else if(this.tiankong>this.xtiankong){
         this.$message.error('填空数量超过，当前数量:'+this.tiankong+"最大可选数量:"+this.xtiankong);
      }
      else if(this.tiankong>this.xtiankong){
         this.$message.error('填空数量超过，当前数量:'+this.tiankong+"最大可选数量:"+this.xtiankong);
      }
      else if(this.jianda>this.xjianda){
         this.$message.error('简答数量超过，当前数量:'+this.jianda+"最大可选数量:"+this.xjianda);
      }
      else if(this.panduan>this.xpanduan){
         this.$message.error('判断数量超过，当前数量:'+this.panduan+"最大可选数量:"+this.xpanduan);
      }
      else if(this.duoxuan>this.xduoxuan){
         this.$message.error('多选数量超过，当前数量:'+this.duoxuan+"最大可选数量:"+this.xduoxuan);
      }
      else if(this.jisuan>this.xjisuan){
         this.$message.error('计算数量超过，当前数量:'+this.jisuan+"最大可选数量:"+this.xjisuan);
      }
      else if(this.lunxu>this.xlunxu){
         this.$message.error('论序数量超过，当前数量:'+this.lunxu+"最大可选数量:"+this.xlunxu);
      }
       else if(this.caozuo>this.xcaozuo){
         this.$message.error('操作数量超过，当前数量:'+this.caozuo+"最大可选数量:"+this.xcaozuo);
      }
      this.dialogVisible = true;
    },
    ExamQie(val) {
      //console.log(val);
      this.examPage = val;
      this.getEaxm();
    },
    getEaxm() {
      let params = {
        page: this.examPage,
        size: this.examSize,
        subjectId: this.subjectId,
        type: this.radio,
      };
      this.postRequest(`/back/practicestores/queryPracticeList`, params).then((res) => {
        this.ExamList = res.data.data.list;
        this.ExamData = res.data.data;
        this.toggleSelection();

        //console.log(res);
      });
    },
    selectEaxm(e) {
      if (!this.zancun[this.radio]) {
        this.zancun[this.radio] = [];
      }
      this.zancun[this.radio][this.examPage] = e;
      this.selectAdd = [];
      this.zancun.forEach((lei) => {
        lei.forEach((xuan) => {
          for (let j = 0; j < xuan.length; j++) {
            this.selectAdd.push(xuan[j]);
          }
        });
      });
      //console.log(this.zancun);
      //console.log(this.selectAdd);
    },
    //消息
    addWord(e) {
      this.messagevisible = true
      this.nowlist =e
 
    },
    addmess(){
      this.subjectId = this.nowlist.subjectId;
      this.getEaxm();
      (this.examId = this.nowlist.examId), (this.AddtEaxm = true);
            this.messagevisible = false

    },
    handleSelectionChange(val) {
      //console.log(val);
      this.multipleSelection = val;
    },
    setbianji(data) {
      this.examId = data.examId;
      this.showAddView = !this.showAddView;
      // let date = this.value1.toLocaleDateString().replace(/\//ig,'-')
      this.subjectId = data.subjectId;
      this.value1 = data.emaxDate;
      this.explainTxt = data.explainTxt;
      this.examName = data.examName;
      let date = data.examPeriod.split("-");
      this.startTime = date[0];
      this.endTime = date[1];
      // examPeriod:this.startTime +'-'+ this.endTime,
      // startTime:date + ' ' + this.startTime + ':00',
      this.zongpoint = data.point;
      this.duration = data.duration;
    },
    // 删除,批量删除已逗号隔开
    deleteObj(e) {
      this.postRequest(`/back/practiceexams/PracticeExam?examIds=${e.examId}`).then(
        (res) => {
          //console.log(res);
          this.$message(res.data.msg);
          this.reload();
        }
      );
    },
    addData() {
      //添加考试
      let date = this.value1.toLocaleDateString().replace(/\//gi, "-");
      let params = {
        classId: this.val2,
        emaxDate: date,
        explainTxt: this.explainTxt,
        examName: this.examName,
        examPeriod: this.startTime + "-" + this.endTime,
        startTime: date + " " + this.startTime + ":00",
        point: this.zongpoint,
        duration: this.duration,
      };
      //console.log(params);
      this.postJson(`/back/practiceexams/addPracticeExam`, params).then((res) => {
        //console.log(res);
        this.$message(res.data.msg);
        this.reload();
      });
    },
    showEditView(shu) {
      // 获取每日一练详情
      this.$router.push({ path: "/Exam/new?id=" + shu.examId });
    },
    setData() {
      let date;
      if (typeof this.value1 == Object) {
        date = this.value1.toLocaleDateString().replace(/\//gi, "-");
      } else {
        date = this.value1;
      }
      let params = {
        classId: this.val2,
        emaxDate: date,
        explainTxt: this.explainTxt,
        examName: this.examName,
        examPeriod: this.startTime + "-" + this.endTime,
        startTime: date + " " + this.startTime + ":00",
        point: this.zongpoint,
        duration: this.duration,
      };
      this.postJson(
        `back/practiceexams/updatePracticeExam?id=${this.examId}`,
        params
      ).then((res) => {
        //console.log(res);
        this.$message(res.data.msg);
        this.reload();
      });
    },
    queryCourse() {
      let that = this;
      return new Promise(function (next, err) {
        that.postRequest("/back/class/queryPracticeClassList").then((res) => {
          // that.postRequest("/comm/subjectCategoryAll").then((res) => {
          //console.log(res);
          // that.course = res.data.data;
          that.options = res.data.data;
          next();
        });
      });
    },
    getdata() {
      let that = this;
      let params = { page: that.page, size: 10 };
      return new Promise(function (next, err) {
        that.postRequest("/back/practiceexams/queryExamList", params).then((res) => {
          //console.log(res);
          that.dailyData = res.data.data;
          that.list = res.data.data.list;
          that.list.forEach((item) => {
            //console.log(item.classId);
            that.options.forEach((value) => {
              //console.log(value);
              value.children.forEach((v) => {
                //console.log(v);
                if (item.classId == v.classId) {
                  item.kemu = v.className;
                }
              });
            });
          });
          //console.log();
          next();
        });
      });
    },
  },
};
</script>

<style>
/* POST /back/circleinfos/query */

</style>
<!-- postRequest -->
